/**
 * 连接符型转驼峰式
 */
function toHumpType (str) {
  return str.replace(/-(\w)/g, function (all, letter) {
    return letter.toUpperCase()
  })
}

/**
 * 驼峰式转连接符型
 */
function toHyphenType (str) {
  return str.replace(/([A-Z])/g, '-$1').toLowerCase()
}

let on
if (document.addEventListener) {
  on = function (element, event, handler) {
    if (element && event && handler) {
      element.addEventListener(event, handler, false)
    }
  }
} else {
  on = function (element, event, handler) {
    if (element && event && handler) {
      element.attachEvent('on' + event, handler)
    }
  }
}

let off
if (document.removeEventListener) {
  off = function (element, event, handler) {
    if (element && event) {
      element.removeEventListener(event, handler, false)
    }
  }
} else {
  off = function (element, event, handler) {
    if (element && event) {
      element.detachEvent('on' + event, handler)
    }
  }
}

export default {
  toHumpType,
  toHyphenType,
  on,
  off
}
